package com.rykj.ruiyan_ptz.ui

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.text.method.DigitsKeyListener
import com.blankj.utilcode.util.SPUtils
import com.rykj.common.ui.BaseActivity
import com.rykj.ruiyan_ptz.BuildConfig
import com.rykj.ruiyan_ptz.R
import com.rykj.ruiyan_ptz.control.UDPManager
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity() {
    var ipStr: String? = null
    var portStr: String? = null

    companion object {
        fun start(activity: Activity) {
            val intent = Intent(activity, LoginActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun isFullScreen(): Boolean {
        return true
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }


    override fun init(savedInstanceState: Bundle?) {
        initEditView()
        initParams()
        connect_btn.setOnClickListener {
            SPUtils.getInstance().put("ip", ipStr, true)
            SPUtils.getInstance().put("port", portStr, true)
            UDPManager.get().init(ipStr!!, portStr?.toInt()!!)
            MainActivity.start(this@LoginActivity)
            finish()
        }
    }

    private fun initParams() {
        if (!TextUtils.isEmpty(SPUtils.getInstance().getString("ip", null))) {
            ipStr = SPUtils.getInstance().getString("ip", "192.168.0.95")
            ip_edit.setText(ipStr)
            isAbleConnect()
        }
        if (!TextUtils.isEmpty(SPUtils.getInstance().getString("port", null))) {
            portStr = SPUtils.getInstance().getString("port", "8083")
            port_edit.setText(portStr)
            isAbleConnect()
        }
    }

    private fun initEditView() {
        ip_edit.inputType = InputType.TYPE_CLASS_NUMBER
        val digits = "0123456789."
        ip_edit.keyListener = DigitsKeyListener.getInstance(digits)
        ip_edit.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(value: CharSequence?, start: Int, before: Int, count: Int) {
                ipStr = value?.toString()
                isAbleConnect()
            }

        })

        port_edit.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(value: CharSequence?, start: Int, before: Int, count: Int) {
                portStr = value?.toString()
                isAbleConnect()
            }
        })
    }

    private fun isAbleConnect() {
        connect_btn.isEnabled = !ipStr.isNullOrEmpty() && !portStr.isNullOrEmpty()
    }

}